<template>
  <!-- 不良组维护 -->
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">

        <p-row>
		  <p-col  :span="24">
			<p-form-item label="不良组编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input readOnly v-decorator="[ 'badGroupBillno', validatorRules.badGroupBillno]" placeholder="请输入不良组编码"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="不良组名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'badGroupName', validatorRules.badGroupName]" placeholder="请输入不良组名称"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="不良组类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-dict-select-tag  type="list" v-decorator="['badGroupType', validatorRules.badGroupType]" :trigger-change="true" dictCode="bad_group_type" placeholder="请选择不良组类型"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-dict-select-tag  type="radio" v-decorator="['status', validatorRules.status]" :trigger-change="true" dictCode="mes_status" placeholder="请选择状态"/>
			</p-form-item>
		  </p-col>
        </p-row>


      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
  import { httpAction, putAction, api } from '@/api/manage'
  import { SubmitOnchangMixin } from '@/mixins/SubmitOnchangMixin'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: "MesQcBadGroupMaintainModal",
    mixins: [SubmitOnchangMixin],
    components: {
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          badGroupBillno: {rules: [
            {required: true, message: '请输入不良组编码!'},
          ]},
          badGroupName: {rules: [
            {required: true, message: '请输入不良组名称!'},
          ]},
          badGroupType: {rules: [
            {required: true, message: '请输入不良组类型!'},
          ]},
          status: {rules: [
            {required: true, message: '请输入状态!'},
          ]},
        },
        url: {
          add: '/mes-quality/mesQcBadGroupMaintain/add',
          edit: '/mes-quality/mesQcBadGroupMaintain/edit',
        }
      }
    },
    created () {
    },
    mounted() {
    this.form.setFieldsValue({ status: 1 });
    this.model.status = 1;
  },

  methods: {
    add() {
      this.edit({});
      this.getRuleCodeValue();
      // this.form.setFieldsValue({ status: 1 });
      // this.model.status = 1;
    },
      getRuleCodeValue() {
        let param = {
          commonFormData: this.model,
          rules: [
            {"ruleCode": "mes_bad_group_rule", "columnName": "badGroupBillno"},
            ]
        }

        putAction(api.ruleBaseURL, param).then(res => {
          // 执行成功，获取返回的值，并赋到页面上
          if (res.success) {
            console.log("生成的编码：" + res.data)
            const item = res.data;
            item['status']=1;//表单默认值赋值区域
            this.form.setFieldsValue(pick(item, Object.keys(item)))
          }
        })
      },

      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'badGroupBillno','badGroupName','badGroupType','createBy','createTime','updateBy','updateTime','status','remark'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
              that.confirmLoading = true;
              let httpurl = '';
              let method = '';
              let formData = Object.assign(this.model, values);
              this.formData = Object.assign(this.model, values);
              //新增触发
              if(!this.model.id){
                httpurl+=this.url.add;
                method = 'post';
                if(this.EnhanceJS.afterAdd){
                  //新增之前
                  let Obj = eval(this.EnhanceJS.afterAdd)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }else{
                //编辑触发
                httpurl+=this.url.edit;
                method = 'put';
                if(this.EnhanceJS.afterEdit){
                 //编辑之前
                  let Obj = eval(this.EnhanceJS.afterEdit)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'badGroupBillno','badGroupName','badGroupType','createBy','createTime','updateBy','updateTime','status','remark'))
      },


    }
  }
</script>
